package io.finer.erp.jeecg.approve.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.approve.entity.ApproveFlow;
import io.finer.erp.jeecg.approve.entity.ApproveFlowNode;
import io.finer.erp.jeecg.approve.service.ApproveFlowNodeService;
import io.finer.erp.jeecg.approve.service.ApproveFlowService;
import io.finer.erp.jeecg.approve.vo.ApproveFlowNodewithMember;
import io.finer.erp.jeecg.approve.vo.ApproveFlowPage;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequest;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequestLine;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 审批流 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-04-14
 */
@Api(tags="审批流")
@RestController
@RequestMapping("/approve/approveFlow")
public class ApproveFlowController {

    @Autowired
    ApproveFlowService approveFlowService;

    @Autowired
    ApproveFlowNodeService approveFlowNodeService;

    @AutoLog(value="审批流-分页列表查询")
    @ApiOperation(value="审批流-分页列表查询", notes="审批流-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(ApproveFlow approveFlow,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<ApproveFlow> queryWrapper = QueryGenerator.initQueryWrapper(approveFlow, req.getParameterMap());
        Page<ApproveFlow> page = new Page<>(pageNo, pageSize);
        IPage<ApproveFlow> pageList = approveFlowService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "审批流-添加")
    @ApiOperation(value="审批流-添加", notes="审批流-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ApproveFlowPage approveFlowPage) {
        ApproveFlow approveFlow = new ApproveFlow();
        BeanUtils.copyProperties(approveFlowPage, approveFlow);
        approveFlowService.saveMain(approveFlow, approveFlowPage.getApproveFlowNodePageList());
        return Result.ok("添加成功！");
    }

    @AutoLog(value = "审批流-编辑")
    @ApiOperation(value="审批流-编辑", notes="审批流-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ApproveFlowPage approveFlowPage) {
        ApproveFlow approveFlow = new ApproveFlow();
        BeanUtils.copyProperties(approveFlowPage, approveFlow);
        ApproveFlow approveFlowEntity = approveFlowService.getById(approveFlow.getId());
        if(approveFlowEntity==null) {
            return Result.error("未找到对应数据");
        }
        approveFlowService.updateMain(approveFlow, approveFlowPage.getApproveFlowNodePageList());
        return Result.ok("编辑成功!");
    }

    @AutoLog(value = "审批流-通过id查询")
    @ApiOperation(value="审批流-通过id查询", notes="审批流-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ApproveFlow approveFlow = approveFlowService.getById(id);
        if(approveFlow==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(approveFlow);
    }

    @AutoLog(value = "审批流节点通过主表ID查询")
    @ApiOperation(value="审批流节点通过主表ID查询", notes="审批流节点-通主表ID查询")
    @GetMapping(value = "/queryLineByMainId")
    public Result<?> queryLineListByMainId(@RequestParam(name="id",required=true) String id) {
//        QueryWrapper<ApproveFlowNode> queryWrapper = new QueryWrapper<>();
        List<ApproveFlowNodewithMember> approveFlowNodewithMemberList = approveFlowNodeService.selectApproveFlowNodewithMemberByMid(id);
        return Result.ok(approveFlowNodewithMemberList);
    }

}

